<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction } from '@api/manage'
import ErpPackageGroupModal from './modules/ErpPackageGroupModal.vue'

export default {
  name: 'ErpPackageGroupList',
  components: { ErpPackageGroupModal },
  mixins: [JeecgListMixin],
  data() {
    return {
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 40,
          align: 'center',
          fixed: 'left',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '包装小组',
          align: 'center',
          dataIndex: 'teamGroup'
        },
        {
          title: '作业工种',
          align: 'center',
          dataIndex: 'groupType_dictText'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 100,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/packagegroup/erpPackageGroup/list',
        delete: '/packagegroup/erpPackageGroup/delete',
        deleteBatch: '/packagegroup/erpPackageGroup/deleteBatch'
      },
      groupId: '',
      groupType: '',
      disableMixinCreated: true,
    }
  },
  props: {
    teamId: {
      type: String,
      default: '',
      required: false
    }
  },
  watch: {
    teamId: {
      immediate: true,
      handler(val) {
        // 确保在 teamId 为空时立即清空数据
        this.dataSource = []
        this.selectedRowKeys = []
        if (val) {
          this.queryParam['teamId'] = val
          this.loadData(1)
        } else {
          // 确保清空分页等状态
          this.clearList()
        }
      }
    }
  },
  methods: {
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
      this.groupId = ''
      this.selectionChange()
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      if (this.selectedMainId === selectedRowKeys[0]) {
        return;
      }
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      this.groupId = selectionRows[0]['id']
      this.groupType = selectionRows[0]['groupType']
      this.selectionChange()
    },
    selectionChange() {
      this.$emit('selection-change', {
        selectedRowKeys: this.selectedRowKeys,
        selectionRows: this.selectionRows,
        groupId: this.groupId,
        groupType: this.groupType
      })
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.onClearSelected()
      const params = this.getQueryParams()
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    clearList() {
      this.dataSource = []
      this.selectedRowKeys = []
      this.selectionRows = []
      this.ipagination.current = 1
      this.ipagination.total = 0
    },
  }
}
</script>

<template>
  <div class="cargo-team-list-container">
    <!-- 操作按钮区域 -->
    <div class="table-operator" v-if="teamId">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>
    <!-- table区域-begin -->
    <div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap1"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type:'radio'}"
        :customRow="clickThenSelect"
        @change="handleTableChange">
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>
    <erp-package-group-modal ref="modalForm" @ok="modalFormOk" :teamId="teamId"/>
  </div>
</template>

<style scoped>
@import '~@assets/less/common.less';
</style>